package com.robot.service;

import com.robot.dto.RunTestContext;
import com.robot.model.BuildTestDo;
import com.robot.model.TestPlanDo;
import com.robot.util.PicReport;
import freemarker.template.Configuration;
import freemarker.template.Template;
import lombok.extern.slf4j.Slf4j;
import org.jfree.data.general.DefaultPieDataset;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import javax.mail.internet.MimeMessage;
import java.io.File;
import java.io.StringWriter;

/**
 * Created with IntelliJ IDEA.
 * User: li
 * Date: 2020/3/29
 * Time: 16:57
 * PROJECT：robot
 * PACKAGE: com.robot
 *
 * @author:55324936@qq.com To change this template use File | Settings | File Templates.
 */
@Slf4j
@Component
public class MailReportService {
    @Autowired
    TestPlanService testPlanService;
    @Value("${spring.mail.from}")
    private String from;
    @Autowired
    FreeMarkerConfigurer freeMarkerConfigurer;
    @Autowired
    JavaMailSender javaMailSender;
    public void sendMail(RunTestContext runTestContext) {
        BuildTestDo buildTestDo = runTestContext.getBuildTestDo();
        Integer testPlanId = buildTestDo.getTestPlanId();
        if (null == testPlanId) {
            return;
        }
        TestPlanDo testPlanDo = testPlanService.getTestPlanById(testPlanId);
        String sendTo = testPlanDo.getSendTo();
        if (StringUtils.isEmpty(sendTo)) {
            return;
        }
        Configuration configuration =freeMarkerConfigurer.getConfiguration();

        try {
            //获取邮件模板里面的内容然后写到字符串里面去，用于邮件正文
            StringWriter stringWriter=new StringWriter();
            Template template = configuration.getTemplate("/mail/mailPage.ftl");
            template.process(buildTestDo,stringWriter);
            String pngPath =buildTestDo.getId()+buildTestDo.getTestPlanId()+"report.png";
            this.pic(buildTestDo,pngPath);
            this.send(sendTo, stringWriter.toString(),pngPath);
        }catch (Exception e){
            log.error("发送测试报告邮件异常",e);
        }
    }
    /**
     * 通过jfree生成饼状图
     * **/
    private void pic(BuildTestDo buildTestDo,String pngPath){
        DefaultPieDataset defaultPieDataSet =new DefaultPieDataset();
        Double passCase =new Double(buildTestDo.getPassCaseSize());
        defaultPieDataSet.setValue("PassCase",passCase);
        defaultPieDataSet.setValue("FailCase",new Double(buildTestDo.getFailCaseSize()));
        PicReport picReport=new PicReport();
        picReport.save(defaultPieDataSet,pngPath,"通过率");
    }

    private void send(String sendTo,String html,String pngPath) throws Exception{

        MimeMessage mimeMessage =javaMailSender.createMimeMessage();
        MimeMessageHelper helper=new MimeMessageHelper(mimeMessage,true);
        helper.setFrom(from);
        if(sendTo.contains(",")){
            String[] to =sendTo.split(",");
            helper.setTo(to);
        }else {
            helper.setTo(sendTo);
        }
        helper.setSubject("robot自动化测试报告");
        FileSystemResource fileSystemResource=new FileSystemResource(new File(pngPath));
        helper.addInline("passRate",fileSystemResource);
        helper.setText(html,Boolean.TRUE);
        javaMailSender.send(mimeMessage);
    }
}
